package com.tlvip.redis.redission;

import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * <p>description：</p>
 * <p>copyright： copyright(C)2016-2099</p>
 * <p>Life is short,we need passion</p>
 * <p>Summary： </p>
 * <p>instructions： </p>
 * Date 3/13/21</p>
 * Author mac
 *
 * @version 1.0
 */
@Service
public class RedissonDemo {
    @Autowired
    private RedissonClient redissonClient;

    public boolean decrementProductStore() {

        String key = "dec_store_lock_" + 222;

        RLock lock = redissonClient.getLock(key);

        try {

            //加锁 操作很类似Java的ReentrantLock机制

            lock.lock();

            System.out.println("lock success!");

        } catch (Exception e) {

            System.out.println(e.getMessage());

        } finally {

            //解锁

            lock.unlock();

        }

        return true;

    }
}
